package com.siwei.mes.entity.system;

import cn.dev33.satoken.stp.StpUtil;
import com.siwei.mes.common.validator.AddGroup;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.config.UidUtils;
import com.siwei.mes.enums.GenerateCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;


/**
 * 供应商(SupplierCompany)实体类
 *
 * @author linzi
 * @since 2024-07-16 16:40:40
 */
@Data
@Accessors(chain = true)
@Table(name = "pk_supplier_company")
@ApiModel(value = "SupplierCompany", description = "供应商")
public class SupplierCompany implements Serializable {
    private static final long serialVersionUID = 485858920411410408L;
    /**
     * 主键id
     */
    @Id
    @ApiModelProperty(value = "供应商主键ID", name = "id")
    @NotNull(groups = {UpdateGroup.class}, message = "供应商主键id为空")
    private Long id;
    /**
     * 供应商编号
     */
    @Column(name = "supplier_no")
    @ApiModelProperty(value = "供应商编号", name = "supplierNo")
    private String supplierNo;
    /**
     * 供应商名称
     */
    @Column(name = "supplier_name")
    @ApiModelProperty(value = "供应商名称", name = "supplierName")
    private String supplierName;
    /**
     * 供应商简称
     */
    @Column(name = "supplier_abbreviation")
    @ApiModelProperty(value = "供应商简称", name = "supplierAbbreviation")
    private String supplierAbbreviation;
    /**
     * 联系人
     */
    @Column(name = "user_name")
    @ApiModelProperty(value = "联系人", name = "userName")
    @NotBlank(groups = {AddGroup.class, UpdateGroup.class}, message = "联系人为空")
    private String userName;
    /**
     * 联系方式
     */
    @Column(name = "user_phone")
    @ApiModelProperty(value = "联系方式", name = "userPhone")
    @NotBlank(groups = {AddGroup.class, UpdateGroup.class}, message = "联系方式为空")
    private String userPhone;
    /**
     * 备案证编号
     */
    @Column(name = "certificate_no")
    @ApiModelProperty(value = "备案证编号", name = "certificateNo")
    @NotBlank(groups = {AddGroup.class, UpdateGroup.class}, message = "备案证编号为空")
    private String certificateNo;

    @Column(name = "certificate_name")
    @ApiModelProperty(value = "备案证名称")
    private String certificateName;
    /**
     * 备案证有效期开始时间
     */
    @Column(name = "certificate_date_start")
    @ApiModelProperty(value = "备案证有效期开始时间", name = "certificateDateStart")
    @NotNull(groups = {AddGroup.class, UpdateGroup.class}, message = "备案证有效期开始时间为空")
    private Date certificateDateStart;
    /**
     * 备案证有效期结束时间
     */
    @Column(name = "certificate_date_end")
    @ApiModelProperty(value = "备案证有效期结束时间", name = "certificateDateEnd")
    @NotNull(groups = {AddGroup.class, UpdateGroup.class}, message = "备案证有效期结束时间为空")
    private Date certificateDateEnd;

    @Column(name = "certificate_img")
    @ApiModelProperty(value = "备案证图片  多个用逗号隔开")
    private String certificateImg;

    @Column(name = "business_license")
    @ApiModelProperty(value = "营业执照 多个用逗号隔开")
    private String businessLicense;
    /**
     * 状态  1-正常 0-禁用
     */
    @Column(name = "status")
    @ApiModelProperty(value = "状态  1-正常 0-禁用", name = "status")
    private Integer status;
    /**
     * 创建人
     */
    @Column(name = "creater")
    @ApiModelProperty(value = "创建人", name = "creater")
    private Long creater;
    /**
     * 创建时间
     */
    @Column(name = "create_time")
    @ApiModelProperty(value = "创建时间", name = "createTime")
    private Date createTime;
    /**
     * 修改人
     */
    @Column(name = "updater")
    @ApiModelProperty(value = "修改人", name = "updater")
    private Long updater;
    /**
     * 更新时间
     */
    @Column(name = "update_time")
    @ApiModelProperty(value = "更新时间", name = "updateTime")
    private Date updateTime;
    /**
     * 是否删除  1-删除
     */
    @Column(name = "is_del")
    @ApiModelProperty(value = "是否删除  1-删除", name = "isDel")
    private Integer isDel;

    @Column(name = "erp_id")
    @ApiModelProperty(value = "对应erpId")
    private Long erpId;

    @Transient
    @ApiModelProperty(value = "材料个数")
    private Long materialCount;

    @Transient
    @ApiModelProperty(value = "提前预警天数")
    private Integer warningDay;

    @Transient
    @ApiModelProperty(value = "预警状态  0-正常 1-预警 2-过期")
    private Integer warningStatus;

    @Transient
    @ApiModelProperty(value = "erp供货单位唯一标识  ycid")
    @NotNull(groups = {UpdateGroup.class}, message = "erp供货单位唯一标识为空")
    private Long erpCompanyId;

    @Transient
    @ApiModelProperty(value = "erp供货单位名称")
    @NotBlank(groups = {UpdateGroup.class}, message = "erp供货单位名称为空")
    private String erpCompanyName;

    public SupplierCompany init(boolean isCreate) {
        this.setUpdater(StpUtil.isLogin() ? StpUtil.getLoginIdAsLong() : null)
                .setUpdateTime(new Date());
        if (isCreate) {
            this.setCreater(StpUtil.isLogin() ? StpUtil.getLoginIdAsLong() : null)
                    .setCreateTime(new Date())
                    .setStatus(1)
                    .setIsDel(0).setSupplierNo(UidUtils.genNo(GenerateCodeEnum.SUPPLIER_COMPANY.getCode()));
        }
        return this;
    }

    public SupplierCompany syncInit(boolean isCreate) {
        this.setUpdateTime(new Date());
        if (isCreate) {
            this.setCreateTime(new Date()).setStatus(1)
                    .setIsDel(0).setSupplierNo(UidUtils.genNo(GenerateCodeEnum.SUPPLIER_COMPANY.getCode()));
        }
        return this;
    }
}
